package com.easy.orm.demo;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.easy.orm.jdbc.JdbcTemplate;
import com.easy.orm.jdbc.transactional.AbstractTransactionalManager;
import com.easy.orm.source.EasyDataSource;

public class TransactionalManagerDemo {

	private static final Logger LOGGER = LoggerFactory.getLogger(TransactionalManagerDemo.class);

	/**
	 * 初始化数据源
	 * 
	 * @return 数据源
	 */
	public DataSource test1DataSource() {
		EasyDataSource dataSource = new EasyDataSource();
		dataSource.setDriver("org.postgresql.Driver");
		dataSource.setUrl("jdbc:postgresql://192.168.2.119:5432/test1");
		dataSource.setUsername("postgres");
		dataSource.setPassword("123456");
		return dataSource;
	}

	/**
	 * 初始化数据源
	 * 
	 * @return 数据源
	 */
	public DataSource testDataSource() {
		EasyDataSource dataSource = new EasyDataSource();
		dataSource.setDriver("org.postgresql.Driver");
		dataSource.setUrl("jdbc:postgresql://192.168.2.119:5432/test");
		dataSource.setUsername("postgres");
		dataSource.setPassword("123456");
		return dataSource;
	}

	/**
	 * 事务
	 */
	public void transaction() {
		JdbcTemplate queryTemplate = new JdbcTemplate(testDataSource());
		JdbcTemplate query1Template = new JdbcTemplate(test1DataSource());

		// 插入
		LOGGER.info("query size {} !", queryTemplate
				.queryForInt(" select count(company_id) from company_profile where company_id = 'company_id_test1'"));
		LOGGER.info("query size {} !", query1Template.queryForInt(
				" select count(a) from a where a = '1000' and b = '1000000' and c = 'company_id_test1_v'"));
		new AbstractTransactionalManager<JdbcTemplate>() {

			@Override
			public void execute() throws RuntimeException {

				JdbcTemplate testTemplate = getJdbcTemplate("test");
				JdbcTemplate test1Template = getJdbcTemplate("test1");
				testTemplate.update("insert into company_profile (company_id) values ('company_id_test1')");
				test1Template.update("insert into a (a,b,c) values ('1000','1000000','company_id_test1_v')");

				LOGGER.info("query size {} !", queryTemplate.queryForInt(
						" select count(company_id) from company_profile where company_id = 'company_id_test1'"));
				LOGGER.info("query size {} !", query1Template.queryForInt(
						" select count(a) from a where a = '1000' and b = '1000000' and c = 'company_id_test1_v'"));
			}

			@Override
			public JdbcTemplate newOperations(Object source) {
				if (source instanceof DataSource)
					try {
						return new JdbcTemplate(((DataSource) source).getConnection());
					} catch (SQLException e) {
						e.printStackTrace();
					}
				return null;
			}

		}.put("test", testDataSource()).put("test1", test1DataSource()).setTransactionName("demo")
				.transactionalExecute();

		LOGGER.info("query size {} !", queryTemplate
				.queryForInt(" select count(company_id) from company_profile where company_id = 'company_id_test1'"));
		LOGGER.info("query size {} !", query1Template.queryForInt(
				" select count(a) from a where a = '1000' and b = '1000000' and c = 'company_id_test1_v'"));

		// 删除错误
		new AbstractTransactionalManager<JdbcTemplate>() {

			@Override
			public void execute() throws RuntimeException {

				JdbcTemplate testTemplate = getJdbcTemplate("test");
				JdbcTemplate test1Template = getJdbcTemplate("test1");
				test1Template.update("delete from a where a = '1000' and b = '1000000' and c = 'company_id_test1_v'");
				testTemplate.update("delete from  company_profile_1");

				LOGGER.info("query size {} !", queryTemplate.queryForInt(
						" select count(company_id) from company_profile where company_id = 'company_id_test1'"));
				LOGGER.info("query size {} !", query1Template.queryForInt(
						" select count(a) from a where a = '1000' and b = '1000000' and c = 'company_id_test1_v'"));
			}

			@Override
			public JdbcTemplate newOperations(Object source) {
				if (source instanceof DataSource)
					try {
						return new JdbcTemplate(((DataSource) source).getConnection());
					} catch (SQLException e) {
						e.printStackTrace();
					}
				return null;
			}

		}.put("test", testDataSource()).put("test1", test1DataSource()).setTransactionName("demo")
				.transactionalExecute();

		LOGGER.info("query size {} !", queryTemplate
				.queryForInt(" select count(company_id) from company_profile where company_id = 'company_id_test1'"));
		LOGGER.info("query size {} !", query1Template.queryForInt(
				" select count(a) from a where a = '1000' and b = '1000000' and c = 'company_id_test1_v'"));

		// 删除成功
		new AbstractTransactionalManager<JdbcTemplate>() {

			@Override
			public void execute() throws RuntimeException {

				JdbcTemplate testTemplate = getJdbcTemplate("test");
				JdbcTemplate test1Template = getJdbcTemplate("test1");
				test1Template.update("delete from a where a = '1000' and b = '1000000' and c = 'company_id_test1_v'");
				testTemplate.update("delete from  company_profile where  company_id = 'company_id_test1'");

				LOGGER.info("query size {} !", queryTemplate.queryForInt(
						" select count(company_id) from company_profile where company_id = 'company_id_test1'"));
				LOGGER.info("query size {} !", query1Template.queryForInt(
						" select count(a) from a where a = '1000' and b = '1000000' and c = 'company_id_test1_v'"));
			}

			@Override
			public JdbcTemplate newOperations(Object source) {
				if (source instanceof DataSource)
					try {
						return new JdbcTemplate(((DataSource) source).getConnection());
					} catch (SQLException e) {
						e.printStackTrace();
					}
				return null;
			}

		}.put("test", testDataSource()).put("test1", test1DataSource()).setTransactionName("demo")
				.transactionalExecute();

		LOGGER.info("query size {} !", queryTemplate
				.queryForInt(" select count(company_id) from company_profile where company_id = 'company_id_test1'"));
		LOGGER.info("query size {} !", query1Template.queryForInt(
				" select count(a) from a where a = '1000' and b = '1000000' and c = 'company_id_test1_v'"));
	}

	public static void main(String[] args) {
		new TransactionalManagerDemo().transaction();
	}

}
